package com.stickerview.core.sticker;

import android.graphics.Matrix;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;

import com.qozix.tileview.plugins.MarkerPlugin;

/**
 * Created by felix on 2017/11/17 下午6:08.
 */

public class IMGStickerMoveHelper{

    private static final String TAG = "IMGStickerMoveHelper";

    private View mView;
    public IMGStickerPortrait.Callback mCallback;

    private float mX, mY;

    private boolean lock = false;

    private static final Matrix M = new Matrix();

    public IMGStickerMoveHelper(View view) {
        mView = view;
    }
    public void lock(){
        this.lock = true ;
    }
    public void unlock(){
        this.lock = false ;
    }
/**
    public boolean onTouch(View v, MotionEvent event) {
        switch (event.getActionMasked()) {
            case MotionEvent.ACTION_DOWN:
                if(lock){
                    return false;
                }
                mX = event.getX();
                mY = event.getY();
                return true;
            case MotionEvent.ACTION_MOVE:
                if(lock){
                    return false;
                }
                float[] dxy = {event.getX() - mX, event.getY() - mY};
                int left = mView.getLeft();
                int top = mView.getTop();
                left += dxy[0];
                top += dxy[1];
                int right = left + mView.getWidth();
                int bottom = top + mView.getHeight();
                mView.layout(left, top, right, bottom);
                mX = event.getX();
                mY = event.getY();
                mCallback.onMove(mView,left,top);
                return true;
        }
        return false;
    }

  **/


     public boolean onTouch(View v, MotionEvent event) {
     switch (event.getActionMasked()) {
     case MotionEvent.ACTION_DOWN:
     mX = event.getX();
     mY = event.getY();
     M.reset();
     M.setRotate(v.getRotation());
     return true;
     case MotionEvent.ACTION_MOVE:
     float[] dxy = {event.getX() - mX, event.getY() - mY};
     M.mapPoints(dxy);
     v.setTranslationX(mView.getTranslationX() + dxy[0]);
     v.setTranslationY(mView.getTranslationY() + dxy[1]);
     mCallback.onMove(mView,  mView.getX(),  mView.getY());
     return true;
     }
     return false;
     }
    public void registerCallback(IMGStickerPortrait.Callback callback) {
        mCallback = callback;
    }

    public void unregisterCallback(IMGStickerPortrait.Callback callback) {
        mCallback = null;
    }

}
